/**
 *
 * @authors garyzyz (zhouyouzhao@yeah.net)
 * @date    2016-12-06 10:36:46
 * 公共对象
 *
 */

;(function (root, factory) {
  if (typeof exports === 'object' && typeof module === 'object') {
    module.exports = factory(exports);
  } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['exports'], factory);
  } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
    // CommonJS
    factory(exports);
  } else {
    // Browser globals
    root.S_Project = factory({});
  }
})((typeof window !== 'undefined' ? window : this), function (exports) {
  'use strict';

  function Project () {
  }

  /*
  * 获取浏览器url
  */
  Project.prototype.getQueryStringObj = function () {
    var qs = (location.search.length > 0 ? location.search.substring(1) : '');
    var args = {};

    var items = qs.length ? qs.split('&') : [];
    var item = null;
    var name = null;
    var value = null;

    var i = 0;
    var len = items.length;

    for (i = 0; i < len; i++) {
      item = items[i].split('=');
      name = decodeURIComponent(item[0]);
      value = decodeURIComponent(item[1]);

      if (name.length) {
        args[name] = value;
      }
    }

    return args;
  };
  /*
    文字信息URL处理
  */
  Project.prototype.setUrl = function (str) {
    /*
      验证是否带有请求头正则
    */
    var reg = /(http:\/\/|https:\/\/)/g;
    /*
      提取URL正则
    */
    var reg1 = /((http:\/\/|https:\/\/)(((\w|=|\?|\/|&|-)+)(\.)((\.|\w|=|\?|\/|&|-)+)))/g;
    var reg2 = /((\w|=|\?|\/|&|-)+)(\.)((\.|\w|=|\?|\/|&|-)+)/g;
    if (reg2.test(str)) {
      if (!reg.test(str)) {
        str = str.replace(str.match(reg2)[0], '<a class="url-style" href="' + 'http://' + str.match(reg2)[0] + '">' + str.match(reg2)[0] + '</a>');
      } else {
        str = str.replace(reg1, '<a class="url-style" href="$1">$1</a>');
      }
    }
    return str;
  };
  /*
    发布/订阅

  */
  Project.prototype.pubSub = function () {
    var fucObj = {};
    var c = false;

    return {
      set: function (newC) {
        c = newC;
      },
      get: function () {
        return c;
      },
      addEvent: function (type, fuc) {
        fucObj[type] = fucObj[type] || [];
        fucObj[type].push(fuc);
      },
      fireEvent: function (type, data) {
        if (!fucObj[type] || fucObj[type].length <= 0) {
          // console.log( '未注册' + type + '事件' );
          return false;
        }

        for (var i = 0; i < fucObj[type].length; i++) {
          fucObj[type][i].apply(this, Array.prototype.slice.call(arguments, 1));
        }
      }
    };
  };
  Project.prototype.callbackFuc = function (callback) {
    if (callback instanceof Array) {
      var len = callback.length;
      for (var i = 0; i < len; i++) {
        callback[i].apply(this, Array.prototype.slice.call(arguments, 1));
      }
    } else {
      callback.apply(this, Array.prototype.slice.call(arguments, 1));
    }
  };

  return Project;
});
